#ifndef MEDIANCUTQUANTIZER_H
#define MEDIANCUTQUANTIZER_H

#include "quantizer.h"

namespace WingGif {

class MedianCutQuantizer : public Quantizer {
  Q_OBJECT
private:
  class MedianCutCube : public QObject {
  public:
    MedianCutCube(QList<QColor> colors);

    int redSize();
    // Length of the red side of the cube.

    int greenSize();
    // Length of the green size of the cube.

    int blueSize();
    // Length of the blue size of the cube.

    QColor color();
    // The mean color of the cube.

    void setPaletteIndex(int newPaletteIndex);
    bool isColorIn(QColor color);

    void splitAtMedian(uchar componentIndex, MedianCutCube *&medianCube1,
                       MedianCutCube *&medianCube2);
    // Splits the cube into 2 smaller cubes using the specified color side for
    // splitting.

  private:
    void shrink();

  public:
    int paletteIndex;

  private:
    uchar _redLowBound;
    uchar _redHighBound;
    uchar _greenLowBound;
    uchar _greenHighBound;
    uchar _blueLowBound;
    uchar _blueHighBound;

    QColor _cubeColor;

    QList<QColor> _colorList;
  };

public:
  MedianCutQuantizer();

  void initialQuantizePixel(QColor pixel) override;
  QVector<QColor> buildPalette() override;
  uchar quantizePixel(QColor pixel) override;

  void splitCubes(QList<MedianCutCube *> &cubes, int count);
  // Splits the list of cubes into smaller cubes until the list one gets the
  // specified size.

protected:
  QList<QColor> colors;
  QList<MedianCutCube *> _cubes;
};

} // namespace WingGif

#endif // MEDIANCUTQUANTIZER_H
